package User;
import java.io.IOException;


import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.regex.Pattern;

import DBModule.CourseTable;
import Interact.Interactable;
import Interact.Interface;
import Test.Course;
import Test.Exception.IllegalFormatException;;

final public class User implements Interactable{
	public static final int ERROR = 0;
	public static final int STUDENT = 1;
	public static final int TEACHER = 2;
	public static final int ADMIN = 3;

	static public int getType(String type){
		if (type.equals("Student")) return STUDENT;
		if (type.equals("Teacher")) return TEACHER;
		if (type.equals("Admin")) return ADMIN;
		return ERROR;
	}
	
	static public String getTypeString(int type){
		String[] types = {"Error", "Student", "Teacher", "Admin"};
		return type > 0 && type < types.length ? types[type] : types[0];
	}
	
	private int type = ERROR;
	private int id = -1;
	private String name = "";
	private String password = "";
	private String dept = "";
	private String phone = "";
	private String email = "";
	private Collection<Course> courses = new TreeSet<Course>();
	
	// 无参构造函数
	public User(){}
	
	protected User(int _type, int _id){
		type = _type;
		id = _id;
	}
	
	// 全构造函数
	protected User(int _type, int _id, String _name, 
			String _password, String _dept, String _phone,
			String _email)
	{
		type		= _type;
		id			= _id;
		name		= _name;
		password	= _password;
		dept 		= _dept;
		phone		= _phone;
		email		= _email;
	}
	
	/** getters */
	public int getID() {return id;}
	public int getType() {return type;}
	public String getPassword() {return password;}
	public String getDept() {return dept;}
	public String getPhone() {return phone;}
	public String getEmail() {return email;}
	public String getName() {return name;}
	
	/** setters */
	public void setID(int id) {this.id = id;}
	public void setType(int type) {this.type = type;}
	public void setPassword(String password) {this.password = password;}
	public void setDept(String dept) {this.dept = dept;}
	public void setPhone(String phone) {this.phone = phone;}
	public void setEmail(String email) {this.email = email;}
	public void setName(String name) {this.name = name;}
	
	public void addCourse(int courseID){
		addCourse(CourseTable.getCourse(courseID));
	}
	
	public void addCourse(Course c){
		if (c != null) courses.add(c);
	}
	
	public void removeCourse(int courseID){
		removeCourse(CourseTable.getCourse(courseID));
	}
	
	public void removeCourse(Course c){
		if (c != null) courses.remove(c);
	}
	
	public Iterator<Course> getCourses(){
		return courses.iterator();
	}
	
	/**
	 * 检查格式
	 * @throws IllegalFormatException	格式不对
	 */
	void checkFormat() throws IllegalFormatException{
		class Local{
			String input, regex, errMsg;
			public Local(String _input, String _regex, String _errMsg){
				input = _input;
				regex = _regex;
				errMsg = _errMsg;
			}
			public boolean check(){
				return Pattern.matches(regex, input);
			}
		}
		
		Local[] checks = new Local[]{
			new Local(Integer.toString(id), "\\d{1,8}", "ID不能超过8位且为纯数字"),
			new Local(name, "[\\u4e00-\\u9fa5]{1,4}", "姓名需为中文字符且不超过4个字符"),
			new Local(password, "\\w{1,20}", "密码不能为空且不超过20个字符"),
			new Local(phone, "1\\d{10}", "手机格式为：1xxxxxxxxxx"),
			new Local(email, ".{1,20}", "E-Mail长度不能超过20个字符"),
			new Local(email, "\\w+@\\w+\\.com", "E-Mail格式错误"),
			new Local(dept, ".{1,10}", "系名不超过10个字符且不为空")
		};
		
		for (Local foo : checks)
			if (!foo.check()) throw new IllegalFormatException(foo.errMsg + " " + foo.input);
	}
	
	@Override
	public void display(Interface out) throws IOException {
		final String pat = "<tr><th>%s</th><td>%s</td></tr>\n";
		class Local{
			String head;
			String body;
			Local(String _head, String _body){
				head = _head;
				body = _body;
			}
			String foo(){return String.format(pat, head, body);}
		}
		
		Local[] foos = {
			new Local("Type", getTypeString(type)),
			new Local("ID", Integer.toString(id)),
			new Local("Name", name),
			new Local("Password", password),
			new Local("Phone", phone),
			new Local("Email", email),
			new Local("Dept", dept),
		};
		
		for (Local foo : foos)
			out.print(foo.foo());
		
		Iterator<Course> iter = getCourses();
		Local foo2 = new Local("Course", "");
		while (iter.hasNext()){
			foo2.body = iter.next().toString();
			out.print(foo2.foo());
		}
	}

	@Override
	public String getKey() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setValueViaInterface(Interface inf) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setMode(int mode) {
		// TODO Auto-generated method stub
		
	}
}













